package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.UserPref;
import ve.com.wayoyo.wuml_case.popup.BetterPopupWindow;
import ve.com.wayoyo.wuml_case.popup.FactoryPopup;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.EditText;

public class VistaDiagrama extends View {
	
	private static float MIN_ZOOM = 0.5f;
	private static float MAX_ZOOM = 3.0f;
	private static VistaDiagrama _instance;
	
	private int _xOffset = 0;
    private int _yOffset = 0;
    private float _Scale = 1.0f;
    private float initialDist;
    
    private int _xTouch = 0;
    private int _yTouch = 0;
    
    private String State = "IDLE";
	
	private Diagrama _diagrama;
	private Paint Fondo;
	private Paint Test;
	private String Estado;
	private ScaleGestureDetector detector;
	private String titulo = null;
	
	
	public VistaDiagrama(Context context,Diagrama diagrama){
		super(context);
		Fondo = new Paint();
		Fondo.setColor(UserPref.getInstancia().getColorFondoDiagramas());
		this._diagrama = diagrama;
//		_diagrama.AgregarComponente(new CompClase("Paparacutimicoro"));
		Test = new Paint();
		Test.setColor(Color.BLACK);
		detector = new ScaleGestureDetector(getContext(), new ScaleListener());
		_instance = this;
	}
	
	public Diagrama getDiagrama(){
		return _diagrama;
	}
	
	public void nuevaClase()
	{
//		String texto = 
//		this._diagrama.AgregarComponente(new CompClase("Paparacutimicoro"+superaux));		
//		this.superaux=superaux+1;
		this.State="NuevaClase";
	}
	
	@Override
	protected void onDraw(Canvas c){
		super.onDraw(c);
		c.drawPaint(Fondo);
		c.drawText(Float.toString(_Scale), 300, 300,Test);
		_diagrama.Dibujar(c,_xOffset , _yOffset, _Scale);
		c.drawText(String.valueOf((int)_Scale), 400, 400,Test);
		_diagrama.Dibujar(c,_xOffset , _yOffset, _Scale);
		//invalidate();
	}
	
	@Override
	public boolean onTouchEvent (MotionEvent event) {
		detector.onTouchEvent(event);
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // store the current touch coordinates for scroll calculation
            _xTouch = (int) event.getX();
            _yTouch = (int) event.getY();
			

			if(State.equals("NuevaClase"))
        	{			
	        	
		        Componente comp = new CompClase("");	

				Point anchor = new Point();
				
				anchor.set((int)((_xTouch)/(_Scale)-_xOffset), (int)((_yTouch)/(_Scale)-_yOffset));
				comp.setAnchor(anchor);
//        		this._diagrama.AgregarComponente(comp);
        		TituloComponente myDialog = new TituloComponente(this.getContext(),"Indique el nombre","Titulo",comp);
		        myDialog.show();
        		State = "Reset";
        		invalidate();
        	}else{
        		_diagrama.resetFocus();
        		State = "IDLE";
        	}
        }else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            // touch starts moving, set the flag
        	if(State == "IDLE"){
	            State = "SCROLLING";
        	}
        	if( State == "SCROLLING"){ 	
	            // get the new offset
        		_xOffset -= _xTouch - (int) event.getX();
        		_yOffset -= _yTouch - (int) event.getY();
        		invalidate();
        	}
    
         // store the last position
            _xTouch = (int) event.getX();
            _yTouch = (int) event.getY();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // touch released
        	//_isScaling = false;
        	initialDist = 0;
        	if(State.equals("IDLE")){
        		Point touchPoint = new Point();
        		touchPoint.set((int)((_xTouch)/(_Scale)-_xOffset), (int)((_yTouch)/(_Scale)-_yOffset));
        		if(_diagrama.checkOnTouch(touchPoint) == true){
        			State = "ItemSelected";
        		}
        		invalidate();
        	}
        	
        }else if(event.getAction() == MotionEvent.ACTION_POINTER_DOWN){
        	
        }
		
		return true;
	}
	
	public void setTitulo(String Titulo) {
		this.titulo = Titulo;
	}
	
	private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

		@Override

		public boolean onScale(ScaleGestureDetector detector) {
				State = "SCALING";
				_Scale *= detector.getScaleFactor();
				_Scale = Math.max(MIN_ZOOM, Math.min(_Scale, MAX_ZOOM));
				invalidate();
			return true;
		}

	}
	
	public static VistaDiagrama getVista(){
		return _instance;
	}
	
	public static void resetFocusStatico(){
		_instance.getDiagrama().resetFocus();
		_instance.invalidate();
	}
	
	

}


